package com.yanq.liancar.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.WindowManager;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.util.Constant;
import com.yanq.liancar.util.Utils;

public class H5Activity extends BaseToolbarActivity {

    public static void start(Context context, String h5Url) {
        Intent intent = new Intent(context, H5Activity.class);
        intent.putExtra(Constant.H5_URL, h5Url);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, true, false);
        setContentView(R.layout.activity_h5);
        mTvTitle.setText("客服");
        String h5Url = getIntent().getStringExtra(Constant.H5_URL);
        if (h5Url != null) {
            setWebData(h5Url);
        }
    }

////------------------------收集BUG-------------------------
//        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(MainActivity.this);
//        strategy.setCrashHandleCallback(new CrashReport.CrashHandleCallback() {
//            public Map<String, String> onCrashHandleStart(int crashType, String errorType, String errorMessage, String errorStack) {
//                LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
//                String x5CrashInfo = com.tencent.smtt.sdk.WebView.getCrashExtraMessage(MainActivity.this);
//                map.put("x5crashInfo", x5CrashInfo);
//                return map;
//            }
//
//
//            @Override
//
//
//
//
//            public byte[] onCrashHandleStart2GetExtraDatas(int crashType, String errorType, String errorMessage, String errorStack) {
//                try {
//                    return "Extra data.".getBytes("UTF-8");
//                } catch (Exception e) {
//                    return null;
//                }
//            }
//        });
//        CrashReport.initCrashReport(MainActivity.this, "此处填写腾讯Bugly上面的appId", true, strategy);
////----------------以上是收集BUG所用代码-----------------------------

    private void setWebData(String url) {
        WebView h5Web = (WebView) findViewById(R.id.h5_web);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        WebSettings settings = h5Web.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setGeolocationEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setGeolocationDatabasePath(this.getFilesDir().getPath());
        h5Web.setWebViewClient(new WebViewClient() {
            /**
             * 防止加载网页时调起系统浏览器
             */
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        h5Web.loadUrl(url);

//        ToastUtil.showProgressDialog(this);
//        WebSettings setting = h5Web.getSettings();
//        setting.setJavaScriptEnabled(true);
//        setting.setBlockNetworkImage(false);
//        setting.setSupportZoom(true);
//        setting.setUseWideViewPort(true);
//        setting.setAllowFileAccess(true);
//        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
//            setting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//        }
//        h5Web.loadUrl(url);
//        h5Web.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }
//
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                ToastUtil.dismissProgressDialog();
//            }
//
//            @Override
//            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//                super.onReceivedError(view, errorCode, description, failingUrl);
//                ToastUtil.dismissProgressDialog();
//            }
//        });
    }
}
